//store的使用方式
import router from './router'
import { getToken, cleanSession } from './utils/auth'
import { menuStore } from '@/store/menu'
//权限验证处理:全集守卫路由实现
const whiteList = ['/login']; //白名单
router.beforeEach(async (to, from, next) => {
    const mstore = menuStore()
    //获取token
    let token = getToken();
    if (token) {
        if (to.path === '/login' || to.path === '/') {
            next({ path: '/' })
        } else {
            // 这个地方怎么设计
            let hasRoles = mstore.menuList && mstore.menuList.length
            if (hasRoles !== 1) {
                next()
            } else {
                try {
                   const res= await mstore.getMenuList(router)
                   console.log(res,'====');
                   
                    next({ ...to, replace: true })
                } catch (error) {
                    console.log('---------');
                    
                    //重置token
                    cleanSession();
                    //跳到登录
                    next({ path: '/login' })
                }
            }

        }
    } else {
        //判断是否在白名单中
        if (whiteList.indexOf(to.path) !== -1) { //存在白名单，放行
            next();
        } else { //不存在，登录
            next({ path: '/login' })
        }
    }
})